package src.queue;

/**
 * 循环队列
 *
 * @author Dream
 */
public class CircularQueue<E> {
    private final Object[] array;
    private int count, head, tail;

    public CircularQueue(int size) {
        array = new Object[size];
    }

    public void add(E value) {
        if (value == null) {
            System.out.println("插入失败内容不能为空");
            return;
        }
        if (count == array.length) {
            System.out.println("插入失败队列已满");
            return;
        }
        array[tail] = value;
        tail++;
        count++;
        if (tail > array.length - 1) {
            tail = 0;
        }

    }

    //移除头部元素
    public E remove() {
        if (isEmpty()) {
            System.out.println("移除失败队列已空");
            return null;
        }
        E e = (E) array[head];
        head++;
        count--;
        if (head > array.length - 1) {
            head = 0;
        }
        return e;
    }

    // 获取头部元素
    public E peek() {
        if (isEmpty()) {
            System.out.println("队列无元素");
            return null;
        }
        return (E) array[head];
    }

    public int size() {
        return count;
    }

    public boolean isEmpty() {
        if (count == 0) {
            return true;
        }
        return false;
    }
}
